//
//  ServerConfig.swift
//  APP
//
//  Created by Rdxer on 2017/12/6.
//  Copyright © 2017年 Rdxer. All rights reserved.
//

import UIKit
import XXLogger
import Device

struct ServerConfig {
    // MARK: - 配置信息
    // url
    public static var baseURLString:String{
        return AppURL.HostURLString
    }
    
    // token
    public static var token:String? = nil{
        didSet{
            printW("刷新了 Token:>>>> \(token ?? "空的")")
        }
    }
    
    public static var page_size:Int = 30
    
    public static var tokenAPI = APIProviderManager.tokenProvider()
    
    public static var tails = "\(Device.version()),\(UIDevice.current.systemName)\(UIDevice.current.systemVersion)"
    
    private init() {}
}

/// 拼接 参数
extension Dictionary {
    
    public func appendIfNotNil(key:String?,value:Any?)->[String : Any]{
        if let key = key,let value = value,var dict = self as? [String : Any] {
            dict[key] = value
            return dict
        }
        return (self as? [String : Any]) ?? [:]
    }
    
    public var appendAllPara:[String : Any]{
        return self.appendToken.appendUserName.appendPageSize
    }
    
    public var appendToken:[String : Any]{
        var dict:[String:Any] = self as! [String : Any]
        if let token = ServerConfig.token {
            dict["token"] = token
        }
        return dict
    }
    
    public var appendUserName:[String : Any]{
        var dict:[String:Any] = self as! [String : Any]
//        if let USERNAME = LoginData.current?.USERNAME {
//            dict["username"] = USERNAME
//        }
        return dict
    }
    
    /// key >> staffNumber
    public var appendStaffNumber:[String : Any]{
        var dict:[String:Any] = self as! [String : Any]
//        if let STAFF_NUMBER = LoginData.current?.STAFF_NUMBER {
//            dict["staffNumber"] = STAFF_NUMBER
//        }
        return dict
    }
    
    /// key >> orgCode
    public var appendOrgCode:[String : Any]{
        var dict:[String:Any] = self as! [String : Any]
//        if let orgCode = LoginData.current?.ORG_CODE {
//            dict["orgCode"] = orgCode
//        }
        return dict
    }
    
    public var appendPageSize:[String : Any]{
        var dict:[String:Any] = self as! [String : Any]
        dict["size"] = ServerConfig.page_size
        return dict
    }
    
    /// 拼接移动端版本
    public var appendTails:[String : Any]{
        var dict:[String:Any] = self as! [String : Any]
        dict["tails"] = ServerConfig.tails
        return dict
    }
    
}


class ServerTools{
    static func check_Resp(_ any:Any)->(res:Bool,message:String,code:Int,resp:[String:Any]?){
        var res = false
        var message = "服务器返回数据异常!解析失败!"
        var code:Int = -99099
        let resp:[String:Any]? = any as? [String:Any]
        
        if let errCode = resp?["errCode"] as? Int {
            if errCode != 0 { // 服务器返回数据异常
                res = false
                code = errCode
                message = (resp?["errMsg"] as? String) ?? message
            }else{ // 成功
                res = true
                message = "操作成功!"
                code = 0
            }
        }
        
        if -99099 == code{
            res = true
            message = "操作成功!"
            code = 0
        }
        
        return (res,message,code,resp)
    }
}








